package com.kehao.accountbook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;

import com.kehao.accountbook.pojo.Expenditure;

import org.w3c.dom.Comment;

import java.util.ArrayList;
import java.util.List;

public class ModifyExpendActivity extends AppCompatActivity implements View.OnClickListener{
    private static final int RESULT_CODE_DELETE = 10;

    boolean flag = true;//收入和支出的标记
    int type = Expenditure.TYPE_NORMAL;
    private Button btnPay;
    private Button btnGet;
    private EditText et_money;
    private EditText et_comment;
    Expenditure expenditure;
    List<ImageView> imageViewList;

    private void init() {
        btnPay = findViewById(R.id.btn_pay);
        btnGet = findViewById(R.id.btn_get);
        et_money = findViewById(R.id.et_money);
        et_comment = findViewById(R.id.et_comment);
        imageViewList = new ArrayList<>();
        imageViewList.add((ImageView) findViewById(R.id.img_normal));
        imageViewList.add((ImageView) findViewById(R.id.img_shopping));
        imageViewList.add((ImageView) findViewById(R.id.img_eatting));
        imageViewList.add((ImageView) findViewById(R.id.img_financial));
        for(ImageView i :imageViewList){
            i.setOnClickListener(ModifyExpendActivity.this);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_expend);
        init();
        Intent intent = getIntent();
        expenditure = (Expenditure) intent.getSerializableExtra("item");
        if(expenditure.getMoney()<0){
            onClickPay(btnPay);
        }else {
            onClickGet(btnGet);
        }
        double money = expenditure.getMoney();
        et_money.setText(String.format("%.2f",(money>0)?money:(-money)));
        switch (expenditure.getType()){
            case Expenditure.TYPE_EATTING:
                onClick(findViewById(R.id.img_eatting));
                break;
            case Expenditure.TYPE_FINANCIAL:
                onClick(findViewById(R.id.img_financial));
                break;
            case Expenditure.TYPE_NORMAL:
                onClick(findViewById(R.id.img_normal));
                break;
            case Expenditure.TYPE_SHOPPING:
                onClick(findViewById(R.id.img_shopping));
                break;
        }
        et_comment.setText(expenditure.getComment());
    }

    public void onClickSubmit(View view) {
        Intent intent = new Intent();
        String money_str = et_money.getText().toString();
        double money;
        String comment = et_comment.getText().toString();
        if(!money_str.equals("")){
            //根据支出还是收入设置正负
            if(flag){
                money = Double.parseDouble("-"+money_str);
            }else {
                money = Double.parseDouble(money_str);
            }
            //数值没有改变的情况下，就算取消了操作
            if(money==expenditure.getMoney()&&type==expenditure.getType()&&expenditure.getComment().equals(comment)){
                setResult(RESULT_CANCELED,intent);
            }else {
                expenditure.setMoney(money);
                expenditure.setType(type);
                expenditure.setComment(comment);
                intent.putExtra("item",expenditure);
                setResult(RESULT_OK,intent);
            }
        }else {
            setResult(RESULT_CANCELED,intent);
        }
        finish();
    }

    public void onClickDelete(View view) {
        Intent intent = new Intent();
        intent.putExtra("item",expenditure);
        setResult(RESULT_CODE_DELETE,intent);
        finish();
    }

    public void onClickGet(View view) {
        flag = false;
        btnPay.setBackgroundColor(Color.parseColor("#EEEEEE"));
        btnGet.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }

    public void onClickPay(View view) {
        flag = true;
        btnPay.setBackgroundColor(Color.parseColor("#FFFFFF"));
        btnGet.setBackgroundColor(Color.parseColor("#EEEEEE"));
    }

    @Override
    public void onClick(View v) {
        for(ImageView i :imageViewList){
            i.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }
        v.setBackgroundColor(Color.parseColor("#F44336"));
        switch (v.getId()){
            case R.id.img_shopping:
                type = Expenditure.TYPE_SHOPPING;
                break;
            case R.id.img_normal:
                type = Expenditure.TYPE_NORMAL;
                break;
            case R.id.img_financial:
                type=Expenditure.TYPE_FINANCIAL;
                break;
            case R.id.img_eatting:
                type=Expenditure.TYPE_EATTING;
                break;
        }
    }


}
